package com.jaux.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jaux.hotel.common.MD5Utils;
import com.jaux.hotel.common.Result;
import com.jaux.hotel.dto.StudentDto;
import com.jaux.hotel.mapper.StudentMapper;
import com.jaux.hotel.pojo.Student;
import com.jaux.hotel.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 16778
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student selectStudentById(Long stuNum) {
        return studentMapper.selectStudentById(stuNum);
    }

    @Override
    public List<Student> selectStudentList() {
        return studentMapper.selectStudentList();
    }

    @Override
    public void deleteStudentById(Long stuNum) {
        studentMapper.deleteStudentById(stuNum);
    }

    @Override
    public String addStudent(Student student) {
        String md5Password = MD5Utils.md5(student.getPassword());
        student.setPassword(md5Password);
        int i = studentMapper.insertStudentInfo(student);
        if(i>=1){
            return student.getName()+"新增成功";
        }
        return student.getName()+"新增失败";
    }

    @Override
    public String updateStuentById(int stuNum, String name) {
        int i = studentMapper.updateStudentById(stuNum, name);
        if(i>=1){
            return "修改成功";
        }
        return "修改失败";
    }

    //@Override
    //public Result<Student> login(int stuNum, String password) {
    //    String md5Pwd = MD5Utils.md5(password);
    //    Student stu = studentMapper.login(stuNum,md5Pwd);
    //    if (stu == null) {
    //        return Result.error("登录失败",404);
    //    }else {
    //        return Result.success(stu,"登录成功");
    //    }
    //}

    @Override
    public Result<String> login(String phone ,String code) {
        // 判断用户输入的手机号和验证码判断用户输入是否正确
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 1. 根据输入的信息查询用户
        lambdaQueryWrapper.eq(Student::getPhone,phone);
        lambdaQueryWrapper.eq(Student::getCode,code);

        // 调用mapper
        Student student = studentMapper.selectOne(lambdaQueryWrapper);
        // 2.如果查询为空，那么说明用户输入的信息错误：登录失败
        if(student == null){
            return Result.error("手机号或验证码输入错误！");
        }else {
            return Result.success("用户登录成功！");
        }

    }


    @Override
    public Result<String> register(Student student) {
        String md5Password = MD5Utils.md5(student.getPassword());
        student.setPassword(md5Password);
        student.setCreateBy(student.getName());
        student.setCreateTime(new Date());
        try {
            studentMapper.insert(student);
            return Result.success("新增成功！");
        }catch (Exception e){
            System.out.println(e);
            return  Result.error("注册失败！");
        }

    }

    @Override
    public Result<String> updatePassword(StudentDto studentDto) {
        // 验证用户输入的验证码是否正确
        // 先根据用户输入的手机号和密码去数据库里面查询用户信息
        // 构建查询对象
        // LambdaQueryWrapper<T>:T->只能是实体entity
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据用户手机号进行查询
        studentLambdaQueryWrapper.eq(Student::getPhone,studentDto.getPhone());
        // 根据用户验证码进行查询
        studentLambdaQueryWrapper.eq(Student::getCode,studentDto.getCode());
        // 使用dao查询
        Student student = studentMapper.selectOne(studentLambdaQueryWrapper);
        // 如果等于空，说明当前用户不存在
        if(student == null){
            return Result.error("手机号或验证码错误！");
            // 如果手机号和验证码正确
        }else {
            // 判断用户两次输入的密码是否正确,相等返回true，不相等返回false
            if(studentDto.getPassword().equals(studentDto.getAgainPassword())){
                // 把刚刚查询出来的用户密码进行修改
                String md5Password = MD5Utils.md5(studentDto.getPassword());
                student.setPassword(md5Password);
                studentMapper.updateById(student);
                return Result.success("用户密码修改成功！");
            }else {
                return Result.error("两次输入的密码不一致，请重新输入！");
            }
        }
    }
}
